﻿#include "FITKCalculiXINPWriter.h"
#include "FITKCCXAdaptorCase.h"
#include "FITKCCXAdaptorModel.h"
#include "FITK_Kernel/FITKAdaptor/FITKIOAdaptorFactory.h"
#include "FITK_Component/FITKAbaqusData/FITKDataCase.h"
#include "FITK_Kernel/FITKAppFramework/FITKMessage.h"

//set集合每行不得超过16个，INP准则
#define SETCOUNT 16

//命名空间
namespace IO
{
    FITKCalculiXINPWriter::FITKCalculiXINPWriter()
    {
    }
    FITKCalculiXINPWriter::~FITKCalculiXINPWriter()
    {
    }

    void FITKCalculiXINPWriter::run()
    {
        //获取文件流
        this->openFileStream();
        auto stream = this->getStream();
        if (stream == nullptr) return;

        //获取算例数据
        AbaqusData::FITKDataCase *caseData =
            AbaqusData::FITKDataCase::SafeDownCastFITKDataCase(_data);
        if (caseData == nullptr) return;

        auto adaptor = FITKIOADAPTORFACTORY->createT<FITKCCXAdaptorCase>("CCXINP", caseData);
        if (adaptor == nullptr)
        {
            this->closeFileStream();
            return;
        }
        adaptor->setDataObject(caseData);
        adaptor->setFileWriter(this);

        if (_resultMark != nullptr)
            *_resultMark = adaptor->adaptW();
        else
            adaptor->adaptW();
        delete adaptor;

        this->closeFileStream();

        this->sendCalculateProgress(100);

    }

    void FITKCalculiXINPWriter::sendCalculateProgress(int Progress)
    {
        emit sendProcessSig(this, Progress);
    }

    bool FITKCalculiXINPWriter::writeLineData(QList<QString> strList)
    {
        QTextStream* stream = this->getStream();
        if (stream == nullptr) return false;

        //超过一行SETCOUNT个则每行SETCOUNT(16)个，否则 maberCount 作为当前行最大长度
        int lineCount = strList.size() > SETCOUNT ? SETCOUNT : strList.size();

        for (int i = 0; i < strList.size(); i += lineCount)
        {
            //写出每行的第一个数据
            *stream << strList[i] << ", ";
            //计算当前行的长度
            lineCount = strList.size() - i > SETCOUNT ? SETCOUNT : strList.size() - i;

            for (int j = 1; j < lineCount; ++j)
            {
                if (j != 1)
                    *stream << ", ";
                *stream << strList[i + j];
            }
            *stream << endl;
        }
        return true;
    }

    void FITKCalculiXINPWriter::consoleMessage(int level, const QString& str)
    {
        switch (level)
        {
            case 1:AppFrame::FITKMessageNormal(str); break;
            case 2: AppFrame::FITKMessageWarning(str); break;
            case 3:AppFrame::FITKMessageError(str); break;
            default: AppFrame::FITKMessageError(str); break;
        }
    }
}



